package com.fled.itsmbigscreen.utils;


import org.apache.commons.codec.binary.Base64;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;

/**
 * HMAC_SHA1 Sign生成器.
 *
 * 需要apache.commons.codec包
 *
 */
public class testHamc {

    private static final String HMAC_SHA1_ALGORITHM = "HmacSHA1";

    /**
     * 使用 HMAC-SHA1 签名方法对data进行签名
     *
     * @param data 被签名的字符串
     * @param key  密钥
     * @return 加密后的字符串
     */
    public static String genHMAC(String data, String key) {
        byte[] result = null;
        try {
            //根据给定的字节数组构造一个密钥,第二参数指定一个密钥算法的名称
            SecretKeySpec signinKey = new SecretKeySpec(key.getBytes(), HMAC_SHA1_ALGORITHM);
            //生成一个指定 Mac 算法 的 Mac 对象
            Mac mac = Mac.getInstance(HMAC_SHA1_ALGORITHM);
            //用给定密钥初始化 Mac 对象
            mac.init(signinKey);
            //完成 Mac 操作
            byte[] rawHmac = mac.doFinal(data.getBytes());
            result = Base64.encodeBase64(rawHmac);

        } catch (NoSuchAlgorithmException e) {
            System.err.println(e.getMessage());
        } catch (InvalidKeyException e) {
            System.err.println(e.getMessage());
        }
        if (null != result) {
            return new String(result);
        } else {
            return null;
        }
    }


    public static String getHmacSHA1(String password,String loginname, String algorithm) {
        byte[] keyBytes = password.getBytes();
        Key key = new SecretKeySpec(keyBytes, 0, keyBytes.length, algorithm);
        Mac mac = null;
        try {
            mac = Mac.getInstance(algorithm);
            mac.init(key);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        }
//        result = Base64.encodeBase64(rawHmac);
        return new String(Base64.encodeBase64(mac.doFinal(loginname.getBytes())));
    }


    /**
     * 16进制加密
     *
     * @param a
     * @return
     */
    protected static String byteArrayToHex(byte[] a) {
        int hn, ln, cx;
        String hexDigitChars = "0123456789abcdef";
        StringBuffer buf = new StringBuffer(a.length * 2);
        for (cx = 0; cx < a.length; cx++) {
            hn = ((int) (a[cx]) & 0x00ff) / 16;
            ln = ((int) (a[cx]) & 0x000f);
            buf.append(hexDigitChars.charAt(hn));
            buf.append(hexDigitChars.charAt(ln));
        }
        return buf.toString();
    }
        /**
         * 测试
         * @param args
         */
        public static void main (String[]args){
//            String genHMAC = genHMAC("\"你的行为和表现还是让我无法完全相信你放下了，小太阳，小月亮", "小太阳小月亮");
//            System.out.println(genHMAC.length()); //28
//            System.out.println(genHMAC);  // O5fviq3DGCB5NrHcl/JP6+xxF6s=

            String genHMAC = getHmacSHA1("小太阳，小月亮","你的行为和表现还是让我无法完全相信你放下了，小太阳，小月亮", "HmacSHA1");
            System.out.println(genHMAC.length()); //28
            System.out.println(genHMAC);  // O5fviq3DGCB5NrHcl/JP6+xxF6s=
        }
    }


